import {createRouter, createWebHistory} from 'vue-router'
import Home from './pages/Home.vue'
import Login from './pages/LoginForm.vue'
import FeedBack from '@/components/employee/FeedBack.vue'
import CourseManagement from './components/employee/CourseManagement.vue'
import ProgressTracking from './components/employee/ProgressTracking.vue'
import UserProfile from './components/employee/UserProfile.vue'
import EmployeeManagement from './components/admin/employeeManagement.vue'
import TrainingManagement from './components/admin/trainingManagement.vue'
import TrackManagement from './components/admin/trackManagement.vue'
import FeedbackManagement from './components/admin/feedbackManagement.vue'
import EmployeeManage from './components/admin/courseManagement.vue'

const routes = [
    {
        path: '/',
        name: 'Login',
        component: Login
    },
    {
        path: '/home',
        name: 'Home',
        component: Home,
        children: [
            // 员工路由
            {
                path: 'employee/feedback',
                name: 'FeedBack',
                component: FeedBack
            },
            {
                path: 'employee/course',
                name: 'CourseManagement',
                component: CourseManagement
            },
            {
                path: 'employee/progress',
                name: 'ProgressTracking',
                component: ProgressTracking
            },
            {
                path: 'employee/user',
                name: 'UserProfile',
                component: UserProfile
            },
            // 管理员路由
            {
                path: 'admin/employeeManagement',
                name: 'EmployeeManagement',
                component: EmployeeManagement
            },
            {
                path: 'admin/employeeManage',
                name: 'EmployeeManage',
                component: EmployeeManage
            },
            {
                path: 'admin/trainingManagement',
                name: 'TrainingManagement',
                component: TrainingManagement
            },
            {
                path: 'admin/trackManagement',
                name: 'TrackManagement',
                component: TrackManagement
            },
            {
                path: 'admin/feedbackManagement',
                name: 'FeedbackManagement',
                component: FeedbackManagement
            }
        ]
    },
    {
        path: '/feedback/:courseId',
        name: 'feedback',
        component: FeedBack,
        props: true
    }
]

const router = createRouter({
    history: createWebHistory(),
    routes
})

// 添加路由守卫
router.beforeEach((to, from, next) => {
    const userType = localStorage.getItem('userType');
    
    // 如果是访问登录页面，直接放行
    if (to.path === '/' || to.path === '/login') {
        next();
        return;
    }
    
    // 如果没有 userType，重定向到登录页
    if (!userType) {
        next('/');
        return;
    }
    
    // 根据用户类型控制访问权限
    if (userType === 'admin') {
        // 管理员只能访问 admin 路径
        if (to.path.includes('/home/admin')) {
            next();
        } else {
            next('/home/admin/employeeManagement');
        }
    } else {
        // 普通用户只能访问 employee 路径
        if (to.path.includes('/home/employee')) {
            next();
        } else {
            next('/home/employee/course');
        }
    }
}); 

export default router